package com.xbongbong.crm.perform.query.render.edit;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.crm.abstracts.AbstractCrmEditPageRenderService;
import com.xbongbong.crm.help.FineClueHelp;
import com.xbongbong.crm.help.PhoneHelp;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.PlatFormEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.pojo.dto.FormDataUpdateGetDTO;
import com.xbongbong.paas.pojo.vo.FormDataUpdateGetVO;
import com.xbongbong.paas.pojo.vo.ParseSingleRowDataVO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.pro.businessdata.pojo.dto.FormatExplainDTO;
import com.xbongbong.pro.enums.DistributorMarkEnum;
import com.xbongbong.pro.enums.FineClueEnum;
import com.xbongbong.pro.formdata.pojo.SaasSpecialParamPojo;
import com.xbongbong.pro.formdata.pojo.dto.SaasAnalysisDataDTO;
import com.xbongbong.pro.formexplain.pojo.vo.HandlerExplainVO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.ClueEnum;
import com.xbongbong.saas.enums.business.CustomerManagementEnum;
import com.xbongbong.saas.model.ClueModel;
import com.xbongbong.saas.model.FormTemplateModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 线索 编辑 页面渲染
 * @author 吴峰
 * @date 2022/06/21 13:58
 */
@Service("clueEditPageRenderServiceImpl")
@Slf4j
public class ClueEditPageRenderServiceImpl extends AbstractCrmEditPageRenderService {

    @Resource
    private ClueModel clueModel;
    @Resource
    private FormTemplateModel formTemplateModel;
    @Resource
    private PhoneHelp phoneHelp;
    @Resource
    private FineClueHelp fineClueHelp;

    @Override
    public List<Integer> businessType() {
        return Arrays.asList(XbbRefTypeEnum.CLUE.getCode());
    }

    @Override
    public ParseSingleRowDataVO formatData(FormDataUpdateGetDTO formDataUpdateGetDTO, HandlerExplainVO handlerExplainVO, PaasFormDataEntityExt paasFormDataEntityExt) throws XbbException {
        SaasAnalysisDataDTO saasAnalysisDataDTO = new SaasAnalysisDataDTO();
        BeanUtil.copyProperties(formDataUpdateGetDTO, saasAnalysisDataDTO);
        // saas特殊格式转换
        JSONObject data = paasFormDataEntityExt.getData();
        Map<String, FieldAttrEntity> explainMap = ExplainUtil.getExplainMapByList(handlerExplainVO.getHeadList());
        saasAnalysisDataDTO.setExplainMap(explainMap);
        clueModel.formatData(saasAnalysisDataDTO, data);
        data.put(FieldTypeEnum.OWNERID.getAlias(), handlerExplainVO.getOwnerId());
        data.put(FieldTypeEnum.COUSERID.getAlias(), handlerExplainVO.getCoUserId());
        if (Objects.equals(data.getInteger(ClueEnum.IS_PUBLIC.getAttr()), 1)) {
            data.put(FieldTypeEnum.OWNERID.getAlias(), new ArrayList<>());
            data.put(FieldTypeEnum.COUSERID.getAlias(), new ArrayList<>());
        }
        data.put(FieldTypeEnum.DEPARTMENTID.getAlias(), paasFormDataEntityExt.getDepartmentId());
        data.put(FieldTypeEnum.SERIALNO.getAlias(), paasFormDataEntityExt.getSerialNo());
        ParseSingleRowDataVO parseSingleRowDataVO = parseSingleRowData(formDataUpdateGetDTO, data, explainMap, handlerExplainVO.getUserMap(), handlerExplainVO.getDepartmentMap());
        // 模板数据
        parseSingleRowDataVO.getResult().put(FieldTypeEnum.TEMPLATE.getAlias(), paasFormDataEntityExt.getFormId());
        // 模板数据
        paasFormDataEntityExt.setData(parseSingleRowDataVO.getResult());
        return parseSingleRowDataVO;
    }

    @Override
    public void businessHandleExplain(FormDataUpdateGetDTO formDataUpdateGetDTO, List<FieldAttrEntity> list, PaasFormEntityExt form, PaasFormDataEntityExt dataEntity) throws XbbException {
        SaasSpecialParamPojo saasSpecialParamPojo = formDataUpdateGetDTO.getSaasSpecialParamPojo();
        boolean formCopy = Objects.nonNull(saasSpecialParamPojo) && Objects.equals(saasSpecialParamPojo.getFromCopy(), BasicConstant.ONE);

        // 编辑时saas字段显示特殊逻辑
        FormatExplainDTO formatExplainDTO = new FormatExplainDTO();
        BeanUtil.copyProperties(formDataUpdateGetDTO, formatExplainDTO);
        formatExplainDTO.setFromCopy(formCopy);
        formatExplainDTO.setResult(dataEntity.getData());
        formatExplainDTO.setExplainList(list);
        clueModel.formatUpdateExplain(formatExplainDTO);

        String corpid = formDataUpdateGetDTO.getCorpid();
        // 设置模板字段
        formTemplateModel.setTemplate(corpid, form, formDataUpdateGetDTO.getBusinessType(), formDataUpdateGetDTO.getLoginUser(), list, 0);
    }

    @Override
    public void handelSaasSpecialUpdate(List<FieldAttrEntity> explainList, Integer businessType, PaasFormDataEntityExt formDataEntityExt, boolean formCopy, Integer distributorMark) throws XbbException {

        String corpid = formDataEntityExt.getCorpid();
        Long formId = formDataEntityExt.getFormId();
        phoneHelp.handlerCustomerRepeat(corpid, formId, explainList, businessType);
    }


    @Override
    public void formatSaasAttrToReturn(FormDataUpdateGetDTO formDataUpdateGetDTO, FormDataUpdateGetVO formDataUpdateGetVO, PaasFormEntityExt form, PaasFormDataEntityExt paasFormDataEntityExt) throws XbbException {
        formDataUpdateGetVO.setData(paasFormDataEntityExt);
        formDataUpdateGetVO.setPaasFormEntity(form);
        //精线索工商查询 通过pid控制icon显示
        String corpid = formDataUpdateGetDTO.getCorpid();
        JSONObject result = formDataUpdateGetVO.getData().getData();
        Integer isFineClue = formDataUpdateGetVO.getIsFineClue();
        String companyName = result.getString("text_1");
        // TODO 精线索
        String pid = fineClueHelp.getPidByCompanyName(corpid, companyName, formDataUpdateGetDTO.getPlatform());
        if (Objects.nonNull(pid)) {
            paasFormDataEntityExt.setPid(pid);
        }


        // TODO 查看是否能使用精线索的工商查询
        boolean fineClueFlag = fineClueHelp.fineClueUsable(corpid, FineClueEnum.BUSINESS) && Objects.equals(DistributorMarkEnum.OTHER.getCode(), formDataUpdateGetDTO.getDistributorMark());
        if (fineClueFlag) {
            formDataUpdateGetVO.setIsFineClue(1);
        }
    }
}
